<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>

<!--搜索框,可以根据用户名和密码搜索-->
<form class="layui-form" lay-filter="search" id="search" style="padding-top: 10px">
    <div class="layui-form-item">
        <!--        layui-inline,外层行内-->

        <div class="layui-inline">
            <label class="layui-form-label">发布人</label>
            <div class="layui-input-inline">
                <input type="text" name="username" placeholder="请输入发布人" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">求购信息</label>
            <!--            layui-input-inline,内层行内-->
            <div class="layui-input-inline">
                <input type="text" name="content" placeholder="请输入求购信息" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-inline">
            <button class="layui-btn" lay-submit lay-filter="formDemo">查询</button>
            <button type="reset" id="searchReset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<!--添加表单-->
<form class="layui-form" action="" style="display: none" id="addPost" lay-filter="editBusinessFormFilter">
    <!-- 这里设置一个隐藏域，在后台管理系统发布的消息默认为管理员发出的 -->
    <input type="hidden" id="addUserId" name="addUserId" value="1">


    <div class="layui-form-item">
        <label class="layui-form-label" description style="width: 100px">求购帖子信息</label>
        <div class="layui-input-inline">
            <textarea id="addContent" name="addContent" placeholder="请输入求购帖子信息" class="layui-textarea"></textarea>
        </div>
    </div>

    <div class="layui-form-item  layui-form" lay-filter="selFilter">
        <label class="layui-form-label" status style="width: 100px">状态</label>
        <div class="layui-input-inline">
            <select id="addStatus" lay-verify="required" name="addStatus">
                <option value="">请选择</option>
                <option value="0">上线</option>
                <option value="1">下线</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="addSubmit">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>


<table class="layui-hide" id="test" lay-filter="demo"></table>


<script src="/layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 -->
<script>
    layui.use(['table', 'jquery', 'form', 'upload'], function () {
        var table = layui.table;
        var $ = layui.$,
            form = layui.form,
            upload = layui.upload;
        var table_Post = table.render({
            elem: '#test'
            , toolbar: '#toolbarDemo'
            , url: '/post/getPage'
            , cols: [
                [
                    {type: 'checkbox'}
                    , {field: 'id', width: 80, title: 'ID', sort: true, align: "center"}
                    , {field: 'username', width: 140, title: '发布人', align: "center"}
                    , {field: 'content', width: 300, title: '求购信息内容'}
                    , {
                    field: 'status', width: 100, title: '信息状态'
                    , templet: function (d) {
                        if (d.status == 0) {
                            return '<button type = "button" class="layui-btn layui-btn layui-btn-xs">正常</button>';
                        } else if (d.status == 1) {
                            return '<button type = "button" class="layui-btn layui-btn-danger layui-btn-xs">下线</button>';
                        }
                    }
                }
                    , {field: 'right', title: '操作', toolbar: '#barDemo', width: 180, align: 'center'}

                ]
            ]
            , page: true
            , limit: 10
            , limits: [5, 10, 15, 20, 25, 30]
        });

        //查询
        form.on('submit(formDemo)', function (data) {
            table.reload('test', {
                where: {
                    username: data.field.username,
                    content: data.field.content

                }
            });
            return false;
        });

        //监听表格复选框选择
        table.on('checkbox(demo)', function (obj) {
            console.log(obj.tr)
        });
        //监听工具条
        table.on('tool(demo)', function (obj) {
            var data = obj.data;

            if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    console.log(data.id)
                    var data_t = {
                        "id": data.id,
                    };
                    $.post("/post/deletePost",
                        {"id": data.id},
                        function (res) {
                            if (res.code == 0) {
                                layer.msg("删除成功");
                                layer.closeAll();
                                table.reload('test', {
                                        url: '/post/getPage',
                                        page: {
                                            curr: 1 //重新从第 1 页开始
                                        },
                                    }
                                );

                            }
                        });

                    layer.close(index);
                });
            } else if (obj.event === 'editUp') {
                $.post("/post/upPost",
                    {"id": data.id},
                    function (res) {
                        if (res.code == 0) {
                            layer.msg("上线成功");
                            layer.closeAll();
                            table.reload('test', {
                                    url: '/post/getPage',
                                    page: {
                                        curr: 1 //重新从第 1 页开始
                                    },
                                }
                            );

                        }
                    });
            } else if (obj.event === 'editDown') {
                $.post("/post/downPost",
                    {"id": data.id},
                    function (res) {
                        if (res.code == 0) {
                            layer.msg("下线成功");
                            layer.closeAll();
                            table.reload('test', {
                                    url: '/post/getPage',
                                    page: {
                                        curr: 1 //重新从第 1 页开始
                                    },
                                }
                            );

                        }
                    });
            }
        });
        <!--toolbar添加删除功能-->
        table.on('toolbar(demo)', function (obj) {
            var check = table.checkStatus(obj.config.id)
                , data = check.data;
            switch (obj.event) {
                case 'add':
                    $("#addPost")[0].reset();
                    layer.open({
                        title: '添加贴子',
                        type: 1,
                        area: ['500px', '300px'],
                        content: $("#addPost")
                    });
                    break;
                case 'dels':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else {
                        var ids = [];
                        for (var i = 0; i < data.length; i++) {
                            ids[i] = data[i].id;
                        }
                        console.log(ids);
                        layer.confirm('真的删除行么', function (index) {
                            $.ajax({
                                url: '/post/batchDeletePost',//路径
                                type: 'post',//post/get
                                data: {ids: ids},//传给后台的参数
                                dataType: 'json',//相应的数据类型,
                                success: function (result) {
                                    //成功响应函数
                                    if (result.code == 0) {
                                        layer.closeAll();
                                        table.reload('test', {
                                                url: '/post/getPage',
                                                page: {
                                                    curr: 1 //重新从第 1 页开始
                                                },
                                            }
                                        );
                                        layer.msg(result.msg, {icon: 6});
                                    } else {
                                        layer.msg(result.msg, {icon: 5});
                                    }
                                }
                            });
                        });
                    }
                    break;
            }
        });


        //监听添加表单的提交
        form.on('submit(addSubmit)', function (data) {
            data = data.field;
            console.log(data);
            var data_t = {

                "content": data.addContent,
                "userId": data.addUserId,
                "status": data.addStatus,
            }
            $.ajax({
                url: "/post/addPost",
                method: "post",
                contentType: 'application/json;charset=UTF-8',
                data: JSON.stringify(data_t),
                success: function (res) {
                    if (res.code == 0) {
                        layer.msg("添加成功");
                        layer.closeAll();
                        table_Post.reload();

                    }
                }
            })
            return false;
        });

    });
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="editUp">上线</a>
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="editDown">下线</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/html" id="toolbarDemo">
    <a class="layui-btn layui-btn-xs" lay-event="add">添加</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="dels">删除</a>
</script>
</body>
</html>